package com.dongjiantong.biz.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.biz.mapper.EarMarksMapper;
import com.dongjiantong.biz.model.EarMarks;
import com.dongjiantong.biz.model.FarmStock;
import com.dongjiantong.biz.service.EarMarksReadeService;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/11/14.
 */

@Slf4j
@Service(version = "1.0.0")
public class EarMarksReadeServicImpl extends ServiceMybatis<EarMarks> implements EarMarksReadeService {

    @Resource
    private EarMarksMapper earMarksMapper;


    @Override
    public PageInfo<EarMarks> selectByMap(Long farmId, int pageNum, int pageSize, List<Integer> statusList, String batch, String farmName, List<Long> farmIdList) {

        Map map = new HashMap();
        if (statusList!=null){
            if (statusList.size()!=0){
                map.put("statusList", statusList);
            }
        }


        if (farmIdList != null) {
            if (farmIdList.size()!=0){
                map.put("farmIdList", farmIdList);
            }

        }
        map.put("farmId", farmId);
        map.put("batch", batch);
        map.put("farmName", farmName);
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(earMarksMapper.selectByMap(map));
    }

    //查询有无重复耳标
    @Override
    public List<EarMarks> selectByRepetitionNumber(List<String> numberMany) {
        try {
            Example example = new Example(EarMarks.class);
            example.createCriteria().andIn("mark_number", numberMany);
            return earMarksMapper.selectByExample(example);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR, "查询异常请稍后再试");
        }

    }

    //
    @Override
    public PageInfo<String> selectBatch(int pageNum, int pageSize, Long farmId) {
        PageHelper.startPage(pageNum, pageSize);
        Map map = new HashMap();
        map.put("farmId", farmId);
        List<String> batch = earMarksMapper.selectBatch(map);
        return new PageInfo<>(batch);
    }

    @Override
    public PageInfo<String> selectBatch(int pageNum, int pageSize, List<Long> farmIdList, String farmName) {
        PageHelper.startPage(pageNum, pageSize);
        Map map = new HashMap();
        map.put("farmIdList", farmIdList);
        map.put("farmName", farmName);
        List<String> batch = earMarksMapper.selectBatch(map);
        return new PageInfo<>(batch);
    }

    @Override
    public List<EarMarks> selectByStatusNumber(String number, Long farmId, Integer sum,Integer status) {
        Map map = new HashMap();
        map.put("farmId", farmId);
        map.put("markNumberStatus", number);
        map.put("sum", sum);
        map.put("status",status);
        return earMarksMapper.selectByMap(map);


    }

    @Override
    public PageInfo<EarMarks> selectByLikeNumber(String number, Long farmId, int pageNum, int pageSize) {
        Map map = new HashMap();
        map.put("farmId", farmId);
        map.put("markNumberLike",number);
        PageHelper.startPage(pageNum,pageSize);
       return new PageInfo<>(earMarksMapper.selectByMap(map));
    }

}
